var windowName = "play";
var Page,downloadManager,dl,likes
apiready = function(){
    onWindowShow(function () {
    })
    onWindowHide(function () {
    })
    downloadManager = api.require('downloadManager');
    console.log("index page is OK");
    dl = new DownLoad()
    likes = new Likes()
    Page = createVM("#Page",{
        //页面数据
        url:""
        ,topic:{
            title:"正在获取信息……"
        }
        ,teacher:{
            intro:"正在获取信息……"
        }
        ,list:[]
        ,title:""
        ,id:0
        ,cur:0
        ,like:false
        ,down:false
        ,dl:{}
    },{
        //页面方法
        "init":function () {
            var data = getData4Page();
            this.url = data.url
            this.title = data.title
            this.id = data.id
            this.cur = data.cur
            // checkStatus()
            // play(this)
            // getData(this)
        }
        ,change: function (id , url,cur,title) {
            this.id = id
            this.url = url
            this.title = title
            this.cur = cur
            play(this)
            checkStatus()
        }
        ,likes:function (what) {
            Page.like = likes.click(
                what
                , function () {
                    apiGet("/app/like/add", {what: what, flag: 0}, function (res) { });
                }
                , function () {
                    apiGet("/app/like/del", {what: what, flag: 0}, function (res) {});
                }
            );
        }
        ,downIt:function (what) {
            dl.add(Page.url,{id:Page.cur,title:Page.title,subject:"数学"})
            Page.down = true
        }
        , cha: function () {



        }
    })
    Page.init()
}

function play(_this) {
    var videoPlayer = api.require('videoPlayer');
    videoPlayer.close()
    videoPlayer.openPlay({
        rect:
            {   x: 0,
                y : 0,
                // w : 320,
                // h: 250
            },
        texts: {
            head: {
                title: _this.title
            }
        },
        styles: {
            head: {
                bg: 'rgba(0.5,0.5,0.5,0.7)',
                height: 24,
                y:5,
                titleSize: 20,
                titleColor: '#fff',
                backSize: 20,
                backImg: 'fs://img/back.png',
                customButtons:[],
            },
            foot: {
                bg: 'rgba(0.5,0.5,0.5,0.7)',
                height: 44,
                playSize: 28,
                playImg: 'fs://img/play.png',
                pauseImg: 'fs://img/pause.png',
                timeSize: 12,
                timeColor: '#fff',
                // sliderImg:'fs://img/slder@2x.png',
                progressColor: '#696969',
                progressSelected: '#76EE00',
                verticalImg:'fs://img/vertical.png',
                horizontalImg:'fs://img/horizontal.png',
                rotationSize:24
            }
        },
        path: _this.url, //（可选项）字符串类型；文档的路径，支持网络和本地（fs://）路径；默认：未传值时不播放
        //在 android 平台上不支持 widget://
        autoPlay: true //（可选项）布尔类型；打开时是否自动播放；默认：true（自动播放）
        ,autorotation:false
    }, function(ret, err) {
        if (ret) {
            log(JSON.stringify(ret));
            if (ret.eventType == "back") {
                goback();
            }


        } else {
            log(JSON.stringify(err));
        }
    });
}


function getData(_this) {
    apiGet("/app/getTopicInfo",{id:_this.id},function (res) {
        log(res)
        _this.teacher = res.d.teacher
        _this.topic = res.d.topic
        _this.list = res.d.list
    });
}

function checkStatus() {
    //下载状态
    Page.down = isTrue(dl.isDown(Page.cur))
    //收藏状态
    Page.like = likes._isLike(Page.cur)
    apiGet("/app/check/like", {what: Page.cur}, function (res) {
        log(Page.cur);
        log(res.d.like);
        log(res.d.down);
        Page.like = res.d.like
        if (Page.like) {
            likes._add(Page.cur)
        }else {
            likes._del(Page.cur)
        }
        // Page.down = res.d.down
    });

}
